#ifndef __MPU9250__H
#define __MPU9250__H
#include <wiringPi.h>
#include <wiringPiI2C.h>
#include <stdio.h>
#include <math.h>

//#include<stdbool.h>
#include<string.h>
#include<time.h>
#include <unistd.h>


typedef unsigned char  bool;
#define true 1
#define false 0

/* define MPU-9250 Device I2C address*/
#define I2C_MPU9250 0x68						// class default I2C address is 0x68
												// specific I2C addresses may be passed as a parameter here
												// AD0 low = 0x68 (default for InvenSense evaluation board)
												// AD0 high = 0x69
	#define MPU6500_ID				0X75  	//MPU6500的器件ID        0X71  
	#define MPU9250_ID				0X71
	#define MPU92_ID				0X70
	
#define I2C_MPU9250_AK8963 		0x0C		//0C ~ 0F
	#define AK8963_ID				0X48	//AK8963的器件ID
#define I2C_MPU9250_AK8963_READ 0x80
#define I2C_MPU9250_AK8963_WRITE 0x00

/* define MPU-9250 Register */
#define REG_SELF_TEST_X_GYRO 	0x00	//陀螺仪自检寄存器X xg_st_data [7:0]  R/W
#define REG_SELF_TEST_Y_GYRO 	0x01	//陀螺仪自检寄存器Y yg_st_data [7:0]	R/W
#define REG_SELF_TEST_Z_GYRO 	0x02	//陀螺仪自检寄存器Z zg_st_data [7:0]	R/W
#define REG_SELF_TEST_X_ACCEL 	0x0D	//加速度自检寄存器X XA_ST_DATA [7:0]	R/W
#define REG_SELF_TEST_Y_ACCEL 	0x0E	//加速度自检寄存器Y YA_ST_DATA [7:0]	R/W
#define REG_SELF_TEST_Z_ACCEL   0x0F	//加速度自检寄存器Z ZA_ST_DATA [7:0]	R/W
#define REG_XG_OFFSET_H   		0x13	//X_OFFS_USR [15:8]	R/W
#define REG_XG_OFFSET_L   		0x14	//X_OFFS_USR [7:0]	R/W
#define REG_YG_OFFSET_H   		0x15	//Y_OFFS_USR [15:8]	R/W
#define REG_YG_OFFSET_L   		0x16	//Y_OFFS_USR [7:0]	R/W
#define REG_ZG_OFFSET_H   		0x17	//Z_OFFS_USR [15:8]	R/W
#define REG_ZG_OFFSET_L   		0x18	//Z_OFFS_USR [7:0]	R/W

#define REG_SMPLRT_DIV   		0x19	//采样频率分频器 SMPLRT_DIV[7:0]	R/W
#define REG_CONFIG		  		0x1A	//配置寄存器 R/W
	#define REG_BIT_FIFO_MODE      		0x40	/* bit[6] */
	#define REG_BIT_EXT_SYNC_SET_0     	0x00	/* bit[5:3] */
	#define REG_BIT_EXT_SYNC_SET_1     	0x08	/* bit[5:3] */
	#define REG_BIT_EXT_SYNC_SET_2    	0x10	/* bit[5:3] */
	#define REG_BIT_EXT_SYNC_SET_3    	0x18	/* bit[5:3] */
	#define REG_BIT_EXT_SYNC_SET_4    	0x20	/* bit[5:3] */
	#define REG_BIT_EXT_SYNC_SET_5    	0x28	/* bit[5:3] */
	#define REG_BIT_EXT_SYNC_SET_6    	0x30	/* bit[5:3] */
	#define REG_BIT_EXT_SYNC_SET_7    	0x38	/* bit[5:3] */
	#define REG_BIT_DLPF_CFG_0          0x00	/* bit[2:0] */
	#define REG_BIT_DLPF_CFG_1          0x01	/* bit[2:0] */
	#define REG_BIT_DLPF_CFG_2          0x02	/* bit[2:0] */
	#define REG_BIT_DLPF_CFG_3          0x03	/* bit[2:0] */
	#define REG_BIT_DLPF_CFG_4          0x04	/* bit[2:0] */
	#define REG_BIT_DLPF_CFG_5          0x05	/* bit[2:0] */
	#define REG_BIT_DLPF_CFG_6          0x06	/* bit[2:0] */
	#define REG_BIT_DLPF_CFG_7          0x07	/* bit[2:0] */
#define REG_GYRO_CONFIG		  	0x1B	//陀螺仪配置寄存器 R/W
	#define REG_BIT_XGYRO_Cten      	0x80	/* bit[7] */
	#define REG_BIT_YGYRO_Cten      	0x40	/* bit[6] */
	#define REG_BIT_ZGYRO_Cten      	0x20	/* bit[5] */
	#define REG_BIT_GYRO_FS_SEL_0     	0x00	/* bit[4:3] */
	#define REG_BIT_GYRO_FS_SEL_1     	0x08	/* bit[4:3] */
	#define REG_BIT_GYRO_FS_SEL_2     	0x10	/* bit[4:3] */
	#define REG_BIT_GYRO_FS_SEL_3     	0x18	/* bit[4:3] */
	#define REG_BIT_FCHOICE_B_0     	0x00	/* bit[1:0] */
	#define REG_BIT_FCHOICE_B_1     	0x01	/* bit[1:0] */
	#define REG_BIT_FCHOICE_B_2     	0x02	/* bit[1:0] */
	#define REG_BIT_FCHOICE_B_3     	0x03	/* bit[1:0] */
#define REG_ACCEL_CONFIG	  	0x1C	//加速度计配置寄存器 RW
	#define REG_BIT_ax_st_en      		0x80	/* bit[7] */
	#define REG_BIT_ay_st_en      		0x40	/* bit[6] */
	#define REG_BIT_az_st_en      		0x20	/* bit[5] */
	#define REG_BIT_ACCEL_FS_SEL_0     	0x00	/* bit[4:3] */
	#define REG_BIT_ACCEL_FS_SEL_1     	0x08	/* bit[4:3] */
	#define REG_BIT_ACCEL_FS_SEL_2     	0x10	/* bit[4:3] */
	#define REG_BIT_ACCEL_FS_SEL_3     	0x18	/* bit[4:3] */
#define REG_ACCEL_CONFIG2		0x1D	//RW
	#define REG_BIT_ACCEL_FCHOICE_B_0   0x00	/* bit[3:2] */
	#define REG_BIT_ACCEL_FCHOICE_B_1   0x04	/* bit[3:2] */
	#define REG_BIT_ACCEL_FCHOICE_B_2   0x08	/* bit[3:2] */
	#define REG_BIT_ACCEL_FCHOICE_B_3   0x0C	/* bit[3:2] */
	#define REG_BIT_A_DLPF_CFG_0 		0x00	/* bit[1:0] */
	#define REG_BIT_A_DLPF_CFG_1 		0x01	/* bit[1:0] */
	#define REG_BIT_A_DLPF_CFG_2 		0x02	/* bit[1:0] */
	#define REG_BIT_A_DLPF_CFG_3 		0x03	/* bit[1:0] */
#define REG_LP_ACCEL_ODR		0x1E	//RW
	#define Lposc_BIT_clkse_0			0x00	/* bit[3:0] */
	#define Lposc_BIT_clkse_1			0x01	/* bit[3:0] */
	#define Lposc_BIT_clkse_2			0x02	/* bit[3:0] */
	#define Lposc_BIT_clkse_3			0x03	/* bit[3:0] */
	#define Lposc_BIT_clkse_4			0x04	/* bit[3:0] */
	#define Lposc_BIT_clkse_5			0x05	/* bit[3:0] */
	#define Lposc_BIT_clkse_6			0x06	/* bit[3:0] */
	#define Lposc_BIT_clkse_7			0x07	/* bit[3:0] */
	#define Lposc_BIT_clkse_8			0x08	/* bit[3:0] */
	#define Lposc_BIT_clkse_9			0x09	/* bit[3:0] */
	#define Lposc_BIT_clkse_10			0x0A	/* bit[3:0] */
	#define Lposc_BIT_clkse_11			0x0B	/* bit[3:0] */
	#define Lposc_BIT_clkse_12			0x0C	/* bit[3:0] */
	#define Lposc_BIT_clkse_13			0x0D	/* bit[3:0] */
	#define Lposc_BIT_clkse_14			0x0E	/* bit[3:0] */
	#define Lposc_BIT_clkse_15			0x0F	/* bit[3:0] */
#define REG_WOM_THR      		0x1F	//运动检测阀值设置寄存器 WOM_Threshold [7:0]  RW
#define REG_FIFO_EN      		0x23	//FIFO使能寄存器      RW
	#define REG_BIT_TEMP_FIFO_EN      	0x80	/* bit[7] */
	#define REG_BIT_GYRO_XOUT     		0x40	/* bit[6] */
	#define REG_BIT_GYRO_YOUT     		0x20	/* bit[5] */
	#define REG_BIT_GYRO_ZOUT      		0x10	/* bit[4] */
	#define REG_BIT_ACCEL     			0x08	/* bit[3] */
	#define REG_BIT_SLV2     			0x04	/* bit[2] */
	#define REG_BIT_SLV1     			0x02	/* bit[1] */
	#define REG_BIT_SLV0     			0x01	/* bit[0] */
	
#define REG_I2C_MST_CTRL      	0x24	//IIC主机控制寄存器 RW
	#define REG_BIT_MULT_MST_EN      	0x80	/* bit[7] */
	#define REG_BIT_WAIT_FOR_ES    		0x40	/* bit[6] */
	#define REG_BIT_SLV_3_FIFO_EN     	0x20	/* bit[5] */
	#define REG_BIT_I2C_MST_P_NSR    	0x10	/* bit[4] */
	#define I2C_MST_CLK_0			0x00	/* bit[3:0] */
	#define I2C_MST_CLK_1			0x01	/* bit[3:0] */
	#define I2C_MST_CLK_2			0x02	/* bit[3:0] */
	#define I2C_MST_CLK_3			0x03	/* bit[3:0] */
	#define I2C_MST_CLK_4			0x04	/* bit[3:0] */
	#define I2C_MST_CLK_5			0x05	/* bit[3:0] */
	#define I2C_MST_CLK_6			0x06	/* bit[3:0] */
	#define I2C_MST_CLK_7			0x07	/* bit[3:0] */
	#define I2C_MST_CLK_8			0x08	/* bit[3:0] */
	#define I2C_MST_CLK_9			0x09	/* bit[3:0] */
	#define I2C_MST_CLK_10			0x0A	/* bit[3:0] */
	#define I2C_MST_CLK_11			0x0B	/* bit[3:0] */
	#define I2C_MST_CLK_12			0x0C	/* bit[3:0] */
	#define I2C_MST_CLK_13			0x0D	/* bit[3:0] */
	#define I2C_MST_CLK_14			0x0E	/* bit[3:0] */
	#define I2C_MST_CLK_15			0x0F	/* bit[3:0] */
	
#define REG_I2C_SLV0_ADDR     	0x25	// IIC从机0器件地址寄存器 RW   
	#define REG_BIT_I2C_SLV0_RNW      	0x80	/* bit[7] */
	//   I2C_ID_0 [6:0]
	
#define REG_I2C_SLV0_REG     	0x26	// IIC从机0数据地址寄存器           I2C_SLV0_REG[7:0]   RW
#define REG_I2C_SLV0_CTRL     	0x27	// IIC从机0控制寄存器	RW
	#define REG_BIT_I2C_SLV0_EN    			0x80	/* bit[7] */
	#define REG_BIT_I2C_SLV0_BYTE_SW    	0x40	/* bit[6] */
	#define REG_BIT_I2C_SLV0_REG_DIS    	0x20	/* bit[5] */
	#define REG_BIT_I2C_SLV0_GRP   			0x10	/* bit[4] */
	#define I2C_SLV0_LENG_0			0x00	/* bit[3:0] */
	#define I2C_SLV0_LENG_1			0x01	/* bit[3:0] */
	#define I2C_SLV0_LENG_2			0x02	/* bit[3:0] */
	#define I2C_SLV0_LENG_3			0x03	/* bit[3:0] */
	#define I2C_SLV0_LENG_4			0x04	/* bit[3:0] */
	#define I2C_SLV0_LENG_5			0x05	/* bit[3:0] */
	#define I2C_SLV0_LENG_6			0x06	/* bit[3:0] */
	#define I2C_SLV0_LENG_7			0x07	/* bit[3:0] */
	#define I2C_SLV0_LENG_8			0x08	/* bit[3:0] */
	#define I2C_SLV0_LENG_9			0x09	/* bit[3:0] */
	#define I2C_SLV0_LENG_10		0x0A	/* bit[3:0] */
	#define I2C_SLV0_LENG_11		0x0B	/* bit[3:0] */
	#define I2C_SLV0_LENG_12		0x0C	/* bit[3:0] */
	#define I2C_SLV0_LENG_13		0x0D	/* bit[3:0] */
	#define I2C_SLV0_LENG_14		0x0E	/* bit[3:0] */
	#define I2C_SLV0_LENG_15		0x0F	/* bit[3:0] */
	
#define REG_I2C_SLV1_ADDR     	0x28	//IIC从机1器件地址寄存器	RW
	#define REG_BIT_I2C_SLV1_RNW      	0x80	/* bit[7] */
	//   I2C_ID_1 [6:0]
	
#define REG_I2C_SLV1_REG     	0x29	//IIC从机1数据地址寄存器	RW 			  I2C_SLV1_REG[7:0]
#define REG_I2C_SLV1_CTRL     	0x2A	//	IIC从机1控制寄存器	RW
	#define REG_BIT_I2C_SLV1_EN    			0x80	/* bit[7] */
	#define REG_BIT_I2C_SLV1_BYTE_SW    	0x40	/* bit[6] */
	#define REG_BIT_I2C_SLV1_REG_DIS    	0x20	/* bit[5] */
	#define REG_BIT_I2C_SLV1_GRP   			0x10	/* bit[4] */
	#define I2C_SLV1_LENG_0			0x00	/* bit[3:0] */
	#define I2C_SLV1_LENG_1			0x01	/* bit[3:0] */
	#define I2C_SLV1_LENG_2			0x02	/* bit[3:0] */
	#define I2C_SLV1_LENG_3			0x03	/* bit[3:0] */
	#define I2C_SLV1_LENG_4			0x04	/* bit[3:0] */
	#define I2C_SLV1_LENG_5			0x05	/* bit[3:0] */
	#define I2C_SLV1_LENG_6			0x06	/* bit[3:0] */
	#define I2C_SLV1_LENG_7			0x07	/* bit[3:0] */
	#define I2C_SLV1_LENG_8			0x08	/* bit[3:0] */
	#define I2C_SLV1_LENG_9			0x09	/* bit[3:0] */
	#define I2C_SLV1_LENG_10		0x0A	/* bit[3:0] */
	#define I2C_SLV1_LENG_11		0x0B	/* bit[3:0] */
	#define I2C_SLV1_LENG_12		0x0C	/* bit[3:0] */
	#define I2C_SLV1_LENG_13		0x0D	/* bit[3:0] */
	#define I2C_SLV1_LENG_14		0x0E	/* bit[3:0] */
	#define I2C_SLV1_LENG_15		0x0F	/* bit[3:0] */
	
#define REG_I2C_SLV2_ADDR     	0x2B	//IIC从机2器件地址寄存器	RW
	#define REG_BIT_I2C_SLV2_RNW      	0x80	/* bit[7] */
	//   I2C_ID_2 [6:0]
	
#define REG_I2C_SLV2_REG     	0x2C	//IIC从机2数据地址寄存器				RW   I2C_SLV2_REG[7:0]
#define REG_I2C_SLV2_CTRL     	0x2D	//IIC从机2控制寄存器	RW
	#define REG_BIT_I2C_SLV2_EN    			0x80	/* bit[7] */
	#define REG_BIT_I2C_SLV2_BYTE_SW    	0x40	/* bit[6] */
	#define REG_BIT_I2C_SLV2_REG_DIS    	0x20	/* bit[5] */
	#define REG_BIT_I2C_SLV2_GRP   			0x10	/* bit[4] */
	#define I2C_SLV2_LENG_0			0x00	/* bit[3:0] */
	#define I2C_SLV2_LENG_1			0x01	/* bit[3:0] */
	#define I2C_SLV2_LENG_2			0x02	/* bit[3:0] */
	#define I2C_SLV2_LENG_3			0x03	/* bit[3:0] */
	#define I2C_SLV2_LENG_4			0x04	/* bit[3:0] */
	#define I2C_SLV2_LENG_5			0x05	/* bit[3:0] */
	#define I2C_SLV2_LENG_6			0x06	/* bit[3:0] */
	#define I2C_SLV2_LENG_7			0x07	/* bit[3:0] */
	#define I2C_SLV2_LENG_8			0x08	/* bit[3:0] */
	#define I2C_SLV2_LENG_9			0x09	/* bit[3:0] */
	#define I2C_SLV2_LENG_10		0x0A	/* bit[3:0] */
	#define I2C_SLV2_LENG_11		0x0B	/* bit[3:0] */
	#define I2C_SLV2_LENG_12		0x0C	/* bit[3:0] */
	#define I2C_SLV2_LENG_13		0x0D	/* bit[3:0] */
	#define I2C_SLV2_LENG_14		0x0E	/* bit[3:0] */
	#define I2C_SLV2_LENG_15		0x0F	/* bit[3:0] */
	
#define REG_I2C_SLV3_ADDR     	0x2E	//IIC从机3器件地址寄存器	RW
	#define REG_BIT_I2C_SLV3_RNW      	0x80	/* bit[7] */
	//   I2C_ID_2 [6:0]
	
#define REG_I2C_SLV3_REG     	0x2F	//IIC从机3数据地址寄存器				RW  I2C_SLV3_REG[7:0]
#define REG_I2C_SLV3_CTRL     	0x30	//IIC从机3控制寄存器	RW
	#define REG_BIT_I2C_SLV3_EN    			0x80	/* bit[7] */
	#define REG_BIT_I2C_SLV3_BYTE_SW    	0x40	/* bit[6] */
	#define REG_BIT_I2C_SLV3_REG_DIS    	0x20	/* bit[5] */
	#define REG_BIT_I2C_SLV3_GRP   			0x10	/* bit[4] */
	#define I2C_SLV3_LENG_0			0x00	/* bit[3:0] */
	#define I2C_SLV3_LENG_1			0x01	/* bit[3:0] */
	#define I2C_SLV3_LENG_2			0x02	/* bit[3:0] */
	#define I2C_SLV3_LENG_3			0x03	/* bit[3:0] */
	#define I2C_SLV3_LENG_4			0x04	/* bit[3:0] */
	#define I2C_SLV3_LENG_5			0x05	/* bit[3:0] */
	#define I2C_SLV3_LENG_6			0x06	/* bit[3:0] */
	#define I2C_SLV3_LENG_7			0x07	/* bit[3:0] */
	#define I2C_SLV3_LENG_8			0x08	/* bit[3:0] */
	#define I2C_SLV3_LENG_9			0x09	/* bit[3:0] */
	#define I2C_SLV3_LENG_10		0x0A	/* bit[3:0] */
	#define I2C_SLV3_LENG_11		0x0B	/* bit[3:0] */
	#define I2C_SLV3_LENG_12		0x0C	/* bit[3:0] */
	#define I2C_SLV3_LENG_13		0x0D	/* bit[3:0] */
	#define I2C_SLV3_LENG_14		0x0E	/* bit[3:0] */
	#define I2C_SLV3_LENG_15		0x0F	/* bit[3:0] */
	
#define REG_I2C_SLV4_ADDR     	0x31	//IIC从机4器件地址寄存器	RW
	#define REG_BIT_I2C_SLV4_RNW      	0x80	/* bit[7] */
	//   I2C_ID_2 [6:0]
	
#define REG_I2C_SLV4_REG     	0x32	//IIC从机4数据地址寄存器	RW	I2C_SLV4_REG[7:0]
#define REG_I2C_SLV4_DO  		0x33	//IIC从机4写数据寄存器			RW	I2C_SLV4_DO[7:0]
#define REG_I2C_SLV4_CTRL	  	0x34	//IIC从机4控制寄存器
	#define REG_BIT_I2C_SLV4_EN    			0x80	/* bit[7] */
	#define REG_BIT_I2C_SLV4_DONE_INT_EN   	0x40	/* bit[6] */
	#define REG_BIT_I2C_SLV4_REG_DISS    	0x20	/* bit[5] */
	#define I2C_MST_DLY_0			0x00	/* bit[4:0] */
	#define I2C_MST_DLY_1			0x01	/* bit[4:0] */
	#define I2C_MST_DLY_2			0x02	/* bit[4:0] */
	#define I2C_MST_DLY_3			0x03	/* bit[4:0] */
	#define I2C_MST_DLY_4			0x04	/* bit[4:0] */
	#define I2C_MST_DLY_5			0x05	/* bit[4:0] */
	#define I2C_MST_DLY_6			0x06	/* bit[4:0] */
	#define I2C_MST_DLY_7			0x07	/* bit[4:0] */
	#define I2C_MST_DLY_8			0x08	/* bit[4:0] */
	#define I2C_MST_DLY_9			0x09	/* bit[4:0] */
	#define I2C_MST_DLY_10		0x0A	/* bit[4:0] */
	#define I2C_MST_DLY_11		0x0B	/* bit[4:0] */
	#define I2C_MST_DLY_12		0x0C	/* bit[4:0] */
	#define I2C_MST_DLY_13		0x0D	/* bit[4:0] */
	#define I2C_MST_DLY_14		0x0E	/* bit[4:0] */
	#define I2C_MST_DLY_15		0x0F	/* bit[4:0] */
	#define I2C_MST_DLY_16		0x10	/* bit[4:0] */
	#define I2C_MST_DLY_17		0x11	/* bit[4:0] */
	#define I2C_MST_DLY_18		0x12	/* bit[4:0] */
	#define I2C_MST_DLY_19		0x13	/* bit[4:0] */
	#define I2C_MST_DLY_20		0x14	/* bit[4:0] */
	#define I2C_MST_DLY_21		0x15	/* bit[4:0] */
	#define I2C_MST_DLY_22		0x16	/* bit[4:0] */
	#define I2C_MST_DLY_23		0x17	/* bit[4:0] */
	#define I2C_MST_DLY_24		0x18	/* bit[4:0] */
	#define I2C_MST_DLY_25		0x19	/* bit[4:0] */
	#define I2C_MST_DLY_26		0x1A	/* bit[4:0] */
	#define I2C_MST_DLY_27		0x1B	/* bit[4:0] */
	#define I2C_MST_DLY_28		0x1C	/* bit[4:0] */
	#define I2C_MST_DLY_29		0x1D	/* bit[4:0] */
	#define I2C_MST_DLY_30		0x1E	/* bit[4:0] */
	#define I2C_MST_DLY_31		0x1F	/* bit[4:0] */
	
#define REG_I2C_SLV4_DI		  	0x35	// IIC从机4读数据寄存器			R   I2C_SLV4_DI[7:0]
#define REG_I2C_MST_STATUS 	  	0x36	// IIC主机状态寄存器	R
	#define REG_BIT_PASS_THROUGH     	0x80	/* bit[7] */
	#define REG_BIT_I2C_SLV4_DONE     	0x40	/* bit[6] */
	#define REG_BIT_I2C_LOST_ARB     		0x20	/* bit[5] */
	#define REG_BIT_I2C_SLV4_NACK     		0x10	/* bit[4] */
	#define REG_BIT_I2C_SLV3_NACK    		0x08	/* bit[3] */
	#define REG_BIT_I2C_SLV2_NACK     		0x04	/* bit[2] */
	#define REG_BIT_I2C_SLV1_NACK     		0x02	/* bit[1] */
	#define REG_BIT_I2C_SLV0_NACK     		0x01	/* bit[0] */
	
#define REG_INT_PIN_CFG 		0x37	//中断/旁路设置寄存器	RW
	#define REG_BIT_ACTL     				0x80	/* bit[7] */
	#define REG_BIT_OPEN    				0x40	/* bit[6] */
	#define REG_BIT_LATCH_INT_EN    		0x20	/* bit[5] */
	#define REG_BIT_INT_ANYRD_2CLEAR     	0x10	/* bit[4] */
	#define REG_BIT_ACTL_FSYNC    			0x08	/* bit[3] */
	#define REG_BIT_FSYNC_INT_MODE_EN     		0x04	/* bit[2] */
	#define REG_BIT_BYPASS_EN     		0x02	/* bit[1] */
	
#define REG_INT_ENABLE          0x38	//中断使能寄存器	RW
	#define REG_BIT_WOM_EN     			0x40	/* bit[6] */
	#define REG_BIT_FIFO_OFLOW_EN    	0x10	/* bit[4] */
	#define REG_BIT_FSYNC_INT_EN     	0x08	/* bit[3] */
	#define REG_BIT_RAW_RDY_EN    		0x01	/* bit[0] */
	
#define REG_INT_STATUS      	0x3A	// 中断状态寄存器	R
	#define REG_BIT_WOM_INT    			0x40	/* bit[6] */
	#define REG_BIT_FIFO_OFLOW_INT    	0x10	/* bit[4] */
	#define REG_BIT_FSYNC_INT     		0x08	/* bit[3] */
	#define REG_BIT_RAW_RDY_INT    		0x01	/* bit[0] */

#define REG_ACCEL_XOUT_H     	0x3B	//加速度值,X轴高8位寄存器	ACCEL_XOUT_H[15:8]  	R
#define REG_ACCEL_XOUT_L      	0x3C	//加速度值,X轴低8位寄存器	ACCEL_XOUT_L[7:0]		R
#define REG_ACCEL_YOUT_H     	0x3D	//加速度值,Y轴高8位寄存器	ACCEL_YOUT_H[15:8]	R
#define REG_ACCEL_YOUT_L     	0x3E	//加速度值,Y轴低8位寄存器	ACCEL_YOUT_L[7:0]		R
#define REG_ACCEL_ZOUT_H     	0x3F	//加速度值,Z轴高8位寄存器	ACCEL_ZOUT_H[15:8]	R
#define REG_ACCEL_ZOUT_L     	0x40	//加速度值,Z轴低8位寄存器	ACCEL_ZOUT_L[7:0]		R
#define REG_TEMP_OUT_H       	0x41	//温度值高八位寄存器	TEMP_OUT_H[15:8]		R
#define REG_TEMP_OUT_L       	0x42	//温度值低8位寄存器	TEMP_OUT_L[7:0]		R
#define REG_GYRO_XOUT_H     	0x43	//陀螺仪值,X轴高8位寄存器 GYRO_XOUT_H[15:8]		R
#define REG_GYRO_XOUT_L     	0x44	//陀螺仪值,X轴低8位寄存器 GYRO_XOUT_L[7:0]		R
#define REG_GYRO_YOUT_H     	0x45	//陀螺仪值,Y轴高8位寄存器 GYRO_YOUT_H[15:8]		R
#define REG_GYRO_YOUT_L     	0x46	//陀螺仪值,Y轴低8位寄存器 GYRO_YOUT_L[7:0]		R
#define REG_GYRO_ZOUT_H     	0x47	//陀螺仪值,Z轴高8位寄存器 GYRO_ZOUT_H[15:8]		R
#define REG_GYRO_ZOUT_L     	0x48	//陀螺仪值,Z轴低8位寄存器 GYRO_ZOUT_L[7:0]		R
#define REG_EXT_SENS_DATA_00    0x49	//EXT_SENS_DATA_00[7:0]	R
#define REG_EXT_SENS_DATA_01    0x4A	//EXT_SENS_DATA_01[7:0]	R
#define REG_EXT_SENS_DATA_02    0x4B	//EXT_SENS_DATA_02[7:0]	R
#define REG_EXT_SENS_DATA_03    0x4C	//EXT_SENS_DATA_03[7:0]	R
#define REG_EXT_SENS_DATA_04    0x4D	//EXT_SENS_DATA_04[7:0]	R
#define REG_EXT_SENS_DATA_05    0x4E	//EXT_SENS_DATA_05[7:0]	R
#define REG_EXT_SENS_DATA_06    0x4F	//EXT_SENS_DATA_06[7:0]	R
#define REG_EXT_SENS_DATA_07    0x50	//EXT_SENS_DATA_07[7:0]	R
#define REG_EXT_SENS_DATA_08    0x51	//EXT_SENS_DATA_08[7:0]	R
#define REG_EXT_SENS_DATA_09    0x52	//EXT_SENS_DATA_09[7:0]	R
#define REG_EXT_SENS_DATA_10    0x53	//EXT_SENS_DATA_10[7:0]	R
#define REG_EXT_SENS_DATA_11    0x54	//EXT_SENS_DATA_11[7:0]	R
#define REG_EXT_SENS_DATA_12    0x55	//EXT_SENS_DATA_12[7:0]	R
#define REG_EXT_SENS_DATA_13    0x56	//EXT_SENS_DATA_13[7:0]	R
#define REG_EXT_SENS_DATA_14    0x57	//EXT_SENS_DATA_14[7:0]	R
#define REG_EXT_SENS_DATA_15    0x58	//EXT_SENS_DATA_15[7:0]	R
#define REG_EXT_SENS_DATA_16    0x59	//EXT_SENS_DATA_16[7:0]	R
#define REG_EXT_SENS_DATA_17    0x5A	//EXT_SENS_DATA_17[7:0]	R
#define REG_EXT_SENS_DATA_18    0x5B	//EXT_SENS_DATA_18[7:0]	R
#define REG_EXT_SENS_DATA_19    0x5C	//EXT_SENS_DATA_19[7:0]	R
#define REG_EXT_SENS_DATA_20    0x5D	//EXT_SENS_DATA_20[7:0]	R
#define REG_EXT_SENS_DATA_21    0x5E	//EXT_SENS_DATA_21[7:0]	R
#define REG_EXT_SENS_DATA_22    0x5F	//EXT_SENS_DATA_22[7:0]	R
#define REG_EXT_SENS_DATA_23    0x60	//EXT_SENS_DATA_23[7:0]	R
#define REG_I2C_SLV0_DO    		0x63	//IIC从机0数据寄存器 I2C_SLV0_DO[7:0]		RW
#define REG_I2C_SLV1_DO    		0x64	//IIC从机1数据寄存器 I2C_SLV1_DO[7:0]		RW
#define REG_I2C_SLV2_DO    		0x65	//IIC从机2数据寄存器 I2C_SLV2_DO[7:0]		RW
#define REG_I2C_SLV3_DO    		0x66	//IIC从机3数据寄存器 I2C_SLV3_DO[7:0]		RW
#define REG_I2C_MST_DELAY_CTRL  0x67	// IIC主机延时管理寄存器 RW
	#define REG_BIT_DELAY_ES_SHADOW     0x80	/* bit[7] */
	#define REG_BIT_I2C_SLV4_DLY_EN     0x10	/* bit[4] */
	#define REG_BIT_I2C_SLV3_DLY_EN     0x08	/* bit[3] */
	#define REG_BIT_I2C_SLV2_DLY_EN     0x04	/* bit[2] */
	#define REG_BIT_I2C_SLV1_DLY_EN     0x02	/* bit[1] */
	#define REG_BIT_I2C_SLV0_DLY_EN     0x01	/* bit[0] */
	
#define REG_SIGNAL_PATH_RESET   0x68	// 信号通道复位寄存器 RW
	#define REG_BIT_GYRO_RST    0x04	/* bit[2] */
	#define REG_BIT_ACCEL_RST   0x02	/* bit[1] */
	#define REG_BIT_TEMP_RST    0x01	/* bit[0] */
	
#define REG_MOT_DETECT_CTRL     0x69	// 运动检测控制寄存器 RW
	#define REG_BIT_ACCEL_INTEL_EN     0x80	/* bit[7] */
	#define REG_BIT_ACCEL_INTEL_MODE   0x40	/* bit[6] */
	
#define REG_USER_CTRL			0x6A	// 用户控制寄存器 RW
	#define REG_BIT_FIFO_EN     0x40	/* bit[6] */
	#define REG_BIT_I2C_MST_EN  0x20	/* bit[5] */
	#define REG_BIT_I2C_IF_DIS  0x10	/* bit[4] */
	#define REG_BIT_FIFO_RST    0x04	/* bit[2] */
	#define REG_BIT_I2C_MST_RST 0x02	/* bit[1] */
	#define REG_BIT_SIG_COND_RST   0x01	/* bit[0] */
	
#define REG_PWR_MGMT_1          0x6B	//	电源管理寄存器1 RW
	#define REG_BIT_H_RESET     0x80	/* bit[7] */
	#define REG_BIT_SLEEP       0x40	/* bit[6] */
	#define REG_BIT_CYCLE       0x20	/* bit[5] */
	#define REG_BIT_GYRO_STANDBY  0x10	/* bit[4] */
	#define REG_BIT_PD_PTAT     0x08	/* bit[3] */
	#define REG_BIT_CLKSEL_7      0x07	/* bit[2:0] */
	#define REG_BIT_CLKSEL_6      0x06	/* bit[2:0] */
	#define REG_BIT_CLKSEL_5      0x05	/* bit[2:0] */
	#define REG_BIT_CLKSEL_4      0x04	/* bit[2:0] */
	#define REG_BIT_CLKSEL_3      0x03	/* bit[2:0] */
	#define REG_BIT_CLKSEL_2      0x02	/* bit[2:0] */
	#define REG_BIT_CLKSEL_1      0x01	/* bit[2:0] */
	#define REG_BIT_CLKSEL_0      0x00	/* bit[2:0] */
	
#define REG_PWR_MGMT_2          0x6C	//	电源管理寄存器2   		RW
	#define REG_BIT_DIS_XA      0x20	/* bit[5] */
	#define REG_BIT_DIS_YA      0x10	/* bit[4] */
	#define REG_BIT_DIS_ZA      0x08	/* bit[3] */
	#define REG_BIT_DIS_XG      0x04	/* bit[2] */
	#define REG_BIT_DIS_YG      0x02	/* bit[1] */
	#define REG_BIT_DIS_ZG      0x01	/* bit[0] */
	
#define REG_FIFO_COUNTH			0x72	//	FIFO计数寄存器高八位	FIFO_CNT[12:8]  	//RW
#define REG_FIFO_COUNTL			0x73	//	FIFO计数寄存器低八位	FIFO_CNT[7:0]		//RW
#define REG_FIFO_R_W			0x74	//	FIFO读写寄存器	RW
#define REG_WHO_AM_I			0x75	//	器件ID寄存器	R
#define REG_XA_OFFSET_H			0x77	//RW
#define REG_XA_OFFSET_L			0x78	//RW
#define REG_YA_OFFSET_H			0x7A	//RW
#define REG_YA_OFFSET_L			0x7B	//RW
#define REG_ZA_OFFSET_H			0x7D	//RW
#define REG_ZA_OFFSET_L			0x7E	//RW

/* define MPU-9250 Register  end */

/* define MPU-9250 MAG Register  */
#define REG_MAG_WIA1 		0x00		// R 
	#define REG_VAL_MAG_WIA1 	0x48	//Device ID of AKM. It is described in one byte and fixed value.  fixed:48H
	#define REG_VAL_MAG_WIA2 	0x9a
	
#define REG_MAG_INFO 		0x01		//R  Device information for AKM.
#define REG_MAG_ST1  		0x02		//R 0:为正常    1:为数据准备或者跳出
	#define REG_BIT_DOR     0x02	/* bit[1] */
	#define REG_BIT_DRDY    0x01	/* bit[0] */
	
#define REG_MAG_DATA_HXL 	0x03   //R
#define REG_MAG_DATA_HXH 	0x04   //R
#define REG_MAG_DATA_HYL 	0x05   //R
#define REG_MAG_DATA_HYH 	0x06   //R
#define REG_MAG_DATA_HZL 	0x07   //R
#define REG_MAG_DATA_HZH 	0x08   //R
#define REG_MAG_ST2  		0x09		//R 0:为正常    1:为数据准备或者跳出
	#define REG_BIT_BITM     	0x10	/* bit[4] */
	#define REG_BIT_HOFL     	0x08	/* bit[3] */
	
#define REG_MAG_CNTL1	 	0x0A	//RW
	#define REG_BIT_MODE3     0x08	/* bit[3] */
	#define REG_BIT_MODE2     0x04	/* bit[2] */
	#define REG_BIT_MODE1     0x02	/* bit[1] */
	#define REG_BIT_MODE0     0x01	/* bit[0] */
	
#define REG_MAG_CNTL2 		0x0B	//RW
	#define REG_BIT_SRST      0x01	/* bit[0] */
	
#define REG_MAG_ASTC		0x0C	//RW
	#define REG_BIT_SELF      0x40	/* bit[6] */
	
#define REG_MAG_TS1 		0x0D	//RW
#define REG_MAG_TS2 		0x0E	//RW
#define REG_MAG_I2CDIS 		0x0F	//RW
	#define REG_BIT_I2CDIS7     0x80	/* bit[7] */
	#define REG_BIT_I2CDIS6     0x40	/* bit[6] */
	#define REG_BIT_I2CDIS5     0x20	/* bit[5] */
	#define REG_BIT_I2CDIS4     0x10	/* bit[4] */
	#define REG_BIT_I2CDIS3     0x08	/* bit[3] */
	#define REG_BIT_I2CDIS2     0x04	/* bit[2] */
	#define REG_BIT_I2CDIS1     0x02	/* bit[1] */
	#define REG_BIT_I2CDIS0     0x01	/* bit[0] */

#define REG_MAG_ASAX 		0x10	//R
	#define REG_BIT_COEFX7      0x80	/* bit[7] */
	#define REG_BIT_COEFX6     0x40	/* bit[6] */
	#define REG_BIT_COEFX5     0x20	/* bit[5] */
	#define REG_BIT_COEFX4     0x10	/* bit[4] */
	#define REG_BIT_COEFX3     0x08	/* bit[3] */
	#define REG_BIT_COEFX2     0x04	/* bit[2] */
	#define REG_BIT_COEFX1     0x02	/* bit[1] */
	#define REG_BIT_COEFX0     0x01	/* bit[0] */
	
#define REG_MAG_ASAY 		0x11	//R
	#define REG_BIT_COEFY7      0x80	/* bit[7] */
	#define REG_BIT_COEFY6     0x40	/* bit[6] */
	#define REG_BIT_COEFY5     0x20	/* bit[5] */
	#define REG_BIT_COEFY4     0x10	/* bit[4] */
	#define REG_BIT_COEFY3     0x08	/* bit[3] */
	#define REG_BIT_COEFY2     0x04	/* bit[2] */
	#define REG_BIT_COEFY1     0x02	/* bit[1] */
	#define REG_BIT_COEFY0     0x01	/* bit[0] */
	
#define REG_MAG_ASAZ 		0x12	//R
	#define REG_BIT_COEFZ7      0x80	/* bit[7] */
	#define REG_BIT_COEFZ6     0x40	/* bit[6] */
	#define REG_BIT_COEFZ5     0x20	/* bit[5] */
	#define REG_BIT_COEFZ4     0x10	/* bit[4] */
	#define REG_BIT_COEFZ3     0x08	/* bit[3] */
	#define REG_BIT_COEFZ2     0x04	/* bit[2] */
	#define REG_BIT_COEFZ1     0x02	/* bit[1] */
	#define REG_BIT_COEFZ0     0x01	/* bit[0] */
	
/* define ICM-20948 MAG Register  end */

#define MAG_DATA_LEN    6

#ifdef __cplusplus
extern "C" {
#endif

typedef unsigned char       uint8;   //  8 bits 
typedef unsigned short int  uint16;  // 16 bits 
typedef unsigned long int   uint32;  // 32 bits 
typedef unsigned long long  uint64;  // 64 bits 

typedef enum 
{  
  IMU_EN_SENSOR_TYPE_NULL = 0,
  IMU_EN_SENSOR_TYPE_ICM20948,
  IMU_EN_SENSOR_TYPE_MPU9250,
  IMU_EN_SENSOR_TYPE_MAX
}IMU_EN_SENSOR_TYPE;
  
#if 0
	typedef struct imu_st_angles_data_tag
	{
  		float fYaw;
  		float fPitch;
  		float fRoll;
	}IMU_ST_ANGLES_DATA;

	typedef struct imu_st_sensor_data_tag
	{
  		short int s16X;
  		short int s16Y;
  		short int s16Z;
	}IMU_ST_SENSOR_DATA;
	typedef enum
	{
		ACC,			//三轴加速度
		GYRO,			//三轴角速度
		MAG,			//三轴磁力计
		TEMP,		  //温度
		STEP,			//步数
		DMP_MPL,	//三态角
	}MPU_DataTeam_TypeDef;

	typedef struct
	{
		short int x;
    	short int y;
    	short int z;
	}INT16_XYZ;

#endif

typedef struct icm20948_st_avg_data_tag
{
	unsigned char  u8Index;
	short int s16AvgBuffer[8];
}ICM20948_ST_AVG_DATA;

typedef struct
{
	int x;
    int y;
    int z;
}INT32_XYZ;


//所有MPU的数据均保存在此结构体内，可以被所有包含了MPU_ForUser.h的文件所调用
#if 0
typedef struct	
{
		short int TEMP;	//温度值
		
		INT16_XYZ
			GYRO,		// 陀螺仪原始数据
			GYRO_Last,	// 陀螺仪历史数据
			ACC, 		// 加速度计原始数据
			ACC_Last,	// 加速度计历史数据
			MAG,		//磁力计原始数据
			MAG_Last,	//磁力计历史数据
			GyroOffset;	//陀螺仪偏移值
	
/************************************************/
//滤波函数需要你们自己添加，添加完成了以后可以激活这三个数据来用
//		FLOAT_XYZ 		
//			GYRO_Real,		// 陀螺仪滤波后的数据
//			ACC_Real,		// 加速度计滤波后的数据
//			MAG_Real;		//磁力计滤波以后的数据
	
		float	
			Pitch,		//俯仰角原始值
			Roll,		//翻滚角原始值
			Yaw,		//偏航角原始值

/************************************************/
		//这里的数据用在三态角重设零点和归为0-360度用，只适用于-180到180的数据，不适用与俯仰角，因为俯仰角范围为-90到90
			Yaw_Real,		//归为0-360度以后的偏航角值
			Yaw_Save,		//保存当前角度的偏航角原始值，用作重设零点
		
			Roll_Real,		//归为0-360度以后的翻滚角值
			Roll_Save,		//保存当前角度的翻滚角原始值，用作重设零点
/************************************************/
		
			Pitch_Last,		//历史俯仰角
			Roll_Last,		//历史翻滚角
			Yaw_Last;		//历史偏航角
		
/************************************************/
unsigned long int	
			Step;			//步数

}MPU_Data_TypeDef; //MPU_Data;
#else

typedef struct mpu_data_tag
{
	short int TEMP;	//温度值
		
	INT32_XYZ
			GYRO,		// 陀螺仪原始数据
			GYRO_Last,	// 陀螺仪历史数据
			ACC, 		// 加速度计原始数据
			ACC_Last,	// 加速度计历史数据
			MAG,		//磁力计原始数据
			MAG_Last,	//磁力计历史数据
			GyroOffset;	//陀螺仪偏移值
	
/************************************************/
//滤波函数需要你们自己添加，添加完成了以后可以激活这三个数据来用
//		FLOAT_XYZ 		
//			GYRO_Real,		// 陀螺仪滤波后的数据
//			ACC_Real,		// 加速度计滤波后的数据
//			MAG_Real;		//磁力计滤波以后的数据
	
		float	
			Pitch,		//俯仰角原始值
			Roll,		//翻滚角原始值
			Yaw,		//偏航角原始值

/************************************************/
		//这里的数据用在三态角重设零点和归为0-360度用，只适用于-180到180的数据，不适用与俯仰角，因为俯仰角范围为-90到90
			Yaw_Real,		//归为0-360度以后的偏航角值
			Yaw_Save,		//保存当前角度的偏航角原始值，用作重设零点
		
			Roll_Real,		//归为0-360度以后的翻滚角值
			Roll_Save,		//保存当前角度的翻滚角原始值，用作重设零点
/************************************************/
		
			Pitch_Last,		//历史俯仰角
			Roll_Last,		//历史翻滚角
			Yaw_Last;		//历史偏航角
		
/************************************************/
unsigned long int	
			Step;			//步数
}Mpu_Data;

#endif

char I2C_ReadOneByte(char reg);
void I2C_WriteOneByte(char reg, char val);

bool imuInit(IMU_EN_SENSOR_TYPE *penMotionSensorType);
bool txtsave(char *str, Mpu_Data *pst);

void MPU9250GyroRead(Mpu_Data *pst);



#endif 
